
public class Solution124 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    /*
     *   1. 遍历顺序？
     *   2. 子层什么情况不能向上传递？
     *
     * */

    //定义一个全局变量
    int max = Integer.MIN_VALUE;

    public int maxPathSum(TreeNode root) {
        //拿到数据才能
        postOder(root);
        return max;
    }

    /*
     *   拿到一边 加上自己 的最大值sum
     * */
    private int postOder(TreeNode node) {
        if (node == null)
            return 0;

        int left = postOder(node.left);
        int right = postOder(node.right);

        //如果你小于0,我还不如不要
        left = Math.max(left, 0);
        right = Math.max(right, 0);

        //最大路径
        max = Math.max(max, right + left + node.val);

        //只返回一边
        return Math.max(left + node.val, right + node.val);
    }


}
